<template>
  <div class="changePwd bgfff">
    <p style="padding: 10px; border-bottom: 1px solid #f2f2f2; font-size: 18px">
      修改密码
    </p>
    <p style="margin: 20px 60px 0px">
      旧密码:
      <el-input
        style="width: 250px"
        placeholder="请输入密码"
        v-model="pwd"
        show-password
      ></el-input>
    </p>
    <p style="margin: 20px 60px 0px">
      新密码:
      <el-input
        style="width: 250px"
        placeholder="请输入新密码"
        v-model="newspwd"
        show-password
      ></el-input>
    </p>
    <p style="margin: 20px 28px 0px">
      确认新密码:
      <el-input
        style="width: 250px"
        placeholder="请确认新密码"
        v-model="surenewPwd"
        show-password
      ></el-input>
    </p>

    <p style="margin: 20px 150px 0px">
      <el-button type="success" @click="sureBtn">确认</el-button>
      <el-button style="background: #ffcc00; color: #fff" @click="resetBtn"
        >重置</el-button
      >
    </p>
  </div>
</template>

<script>
import { checkoldpwd, editpwd } from "@/apis/users";
export default {
  data() {
    return {
      userId: localStorage.getItem("userId"), //用户id
      pwd: "",
      newspwd: "",
      surenewPwd: "",
    };
  },
  methods: {
    //点击确定
    sureBtn() {
      //旧密码验证
      checkoldpwd({
        oldPwd: this.pwd,
        id: this.userId,
      }).then((res) => {
        // console.log(res.data);
        if (res.data.code == 1) {
          this.$message({
            showClose: true,
            message: "系统提示：旧密码错误 (っ °Д °;)",
            type: "error",
          });
        } else if (res.data.code == 0) {
          //判定新密码
          if (this.newspwd != this.surenewPwd) {
            this.$message({
              showClose: true,
              message: "系统提示：两次输入的密码不同 っ╥﹏╥...",
              type: "error",
            });
          } else if (!this.newspwd || !this.surenewPwd) {
            this.$message({
              showClose: true,
              message: "系统提示：密码不能为空 っ╥﹏╥...",
              type: "error",
            });
          } else {
            //修改密码
            editpwd({
              newPwd: this.surenewPwd,
              id: this.userId,
            }).then((res) => {
              // console.log(res.data);
              if (res.data.code == 0) {
                this.$message({
                  showClose: true,
                  message: "系统提示：修改成功！请重新登录 ヾ(•ω•`)o",
                  type: "success",
                });
                this.$router.push("/");
              }
            });
          }
        }
      });
    },
    //点击重置
    resetBtn() {
      (this.pwd = ""), (this.newspwd = ""), (this.surenewPwd = "");
    },
  },
  created(){
     this.$bus.emit('subTitle','帐号管理>','修改密码>')
  }
};
</script>

<style lang="less" scoped>
.changePwd {
  padding-bottom: 40px;
}
</style>